home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / spamexperts / dis.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  6.8 KB  |  271 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. """Disassembler of Python byte code into mnemonics.
  5.  
  6. This is Python's dis module, but without forcing print to stdout.
  7. """
  8. import sys
  9. import types
  10. from opcode import *
  11. from opcode import __all__ as _opcodes_all
  12. __all__ = [
  13.     'dis',
  14.     'disassemble',
  15.     'distb',
  16.     'disco'] + _opcodes_all
  17. del _opcodes_all
  18.  
  19. def dis(x = None, out = None):
  20.     '''Disassemble classes, methods, functions, or code.
  21.  
  22.     With no argument, disassemble the last traceback.
  23.     '''
  24.     if out is None:
  25.         print 'DEFAULT TO STDOUT'
  26.         out = sys.stdout
  27.     
  28.     if x is None:
  29.         distb()
  30.         return None
  31.     
  32.     if type(x) is types.InstanceType:
  33.         x = x.__class__
  34.     
  35.     if hasattr(x, 'im_func'):
  36.         x = x.im_func
  37.     
  38.     if hasattr(x, 'func_code'):
  39.         x = x.func_code
  40.     
  41.     if hasattr(x, '__dict__'):
  42.         items = x.__dict__.items()
  43.         items.sort()
  44.         for name, x1 in items:
  45.             if type(x1) in (types.MethodType, types.FunctionType, types.CodeType, types.ClassType):
  46.                 print >>out, 'Disassembly of %s:' % name
  47.                 
  48.                 try:
  49.                     dis(x1, out)
  50.                 except TypeError:
  51.                     msg = None
  52.                     print >>out, 'Sorry:', msg
  53.  
  54.                 print >>out
  55.                 continue
  56.         
  57.     elif hasattr(x, 'co_code'):
  58.         disassemble(x, out = out)
  59.     elif isinstance(x, str):
  60.         disassemble_string(x, out = out)
  61.     else:
  62.         raise TypeError, "don't know how to disassemble %s objects" % type(x).__name__
  63.  
  64.  
  65. def distb(tb = None):
  66.     '''Disassemble a traceback (default: last traceback).'''
  67.     if tb is None:
  68.         
  69.         try:
  70.             tb = sys.last_traceback
  71.         except AttributeError:
  72.             raise RuntimeError, 'no last traceback to disassemble'
  73.  
  74.         while tb.tb_next:
  75.             tb = tb.tb_next
  76.     
  77.     disassemble(tb.tb_frame.f_code, tb.tb_lasti)
  78.  
  79.  
  80. def disassemble(co, lasti = -1, out = None):
  81.     '''Disassemble a code object.'''
  82.     if out is None:
  83.         print 'DEFAULT TO STDOUT'
  84.         out = sys.stdout
  85.     
  86.     code = co.co_code
  87.     labels = findlabels(code)
  88.     linestarts = dict(findlinestarts(co))
  89.     n = len(code)
  90.     i = 0
  91.     extended_arg = 0
  92.     free = None
  93.     while i < n:
  94.         c = code[i]
  95.         op = ord(c)
  96.         if i in linestarts:
  97.             if i > 0:
  98.                 print >>out
  99.             
  100.             print >>out, '%3d' % linestarts[i],
  101.         else:
  102.             print >>out, '   ',
  103.         if i == lasti:
  104.             print >>out, '-->',
  105.         else:
  106.             print >>out, '   ',
  107.         if i in labels:
  108.             print >>out, '>>',
  109.         else:
  110.             print >>out, '  ',
  111.         print >>out, repr(i).rjust(4),
  112.         print >>out, opname[op].ljust(20),
  113.         i = i + 1
  114.         if op >= HAVE_ARGUMENT:
  115.             oparg = ord(code[i]) + ord(code[i + 1]) * 256 + extended_arg
  116.             extended_arg = 0
  117.             i = i + 2
  118.             if op == EXTENDED_ARG:
  119.                 extended_arg = oparg * 0x10000L
  120.             
  121.             print >>out, repr(oparg).rjust(5),
  122.             if op in hasconst:
  123.                 print >>out, '(' + repr(co.co_consts[oparg]) + ')',
  124.             elif op in hasname:
  125.                 print >>out, '(' + co.co_names[oparg] + ')',
  126.             elif op in hasjrel:
  127.                 print >>out, '(to ' + repr(i + oparg) + ')',
  128.             elif op in haslocal:
  129.                 print >>out, '(' + co.co_varnames[oparg] + ')',
  130.             elif op in hascompare:
  131.                 print >>out, '(' + cmp_op[oparg] + ')',
  132.             elif op in hasfree:
  133.                 if free is None:
  134.                     free = co.co_cellvars + co.co_freevars
  135.                 
  136.                 print >>out, '(' + free[oparg] + ')',
  137.             
  138.         
  139.         print >>out
  140.  
  141.  
  142. def disassemble_string(code, lasti = -1, varnames = None, names = None, constants = None, out = None):
  143.     if out is None:
  144.         print 'DEFAULT TO STDOUT'
  145.         out = sys.stdout
  146.     
  147.     labels = findlabels(code)
  148.     n = len(code)
  149.     i = 0
  150.     while i < n:
  151.         c = code[i]
  152.         op = ord(c)
  153.         if i == lasti:
  154.             print >>out, '-->',
  155.         else:
  156.             print >>out, '   ',
  157.         if i in labels:
  158.             print >>out, '>>',
  159.         else:
  160.             print >>out, '  ',
  161.         print >>out, repr(i).rjust(4),
  162.         print >>out, opname[op].ljust(15),
  163.         i = i + 1
  164.         if op >= HAVE_ARGUMENT:
  165.             oparg = ord(code[i]) + ord(code[i + 1]) * 256
  166.             i = i + 2
  167.             print >>out, repr(oparg).rjust(5),
  168.             if op in hasconst:
  169.                 if constants:
  170.                     print >>out, '(' + repr(constants[oparg]) + ')',
  171.                 else:
  172.                     print >>out, '(%d)' % oparg,
  173.             elif op in hasname:
  174.                 if names is not None:
  175.                     print >>out, '(' + names[oparg] + ')',
  176.                 else:
  177.                     print >>out, '(%d)' % oparg,
  178.             elif op in hasjrel:
  179.                 print >>out, '(to ' + repr(i + oparg) + ')',
  180.             elif op in haslocal:
  181.                 if varnames:
  182.                     print >>out, '(' + varnames[oparg] + ')',
  183.                 else:
  184.                     print >>out, '(%d)' % oparg,
  185.             elif op in hascompare:
  186.                 print >>out, '(' + cmp_op[oparg] + ')',
  187.             
  188.         
  189.         print >>out
  190.  
  191. disco = disassemble
  192.  
  193. def findlabels(code):
  194.     '''Detect all offsets in a byte code which are jump targets.
  195.  
  196.     Return the list of offsets.
  197.  
  198.     '''
  199.     labels = []
  200.     n = len(code)
  201.     i = 0
  202.     while i < n:
  203.         c = code[i]
  204.         op = ord(c)
  205.         i = i + 1
  206.         if op >= HAVE_ARGUMENT:
  207.             oparg = ord(code[i]) + ord(code[i + 1]) * 256
  208.             i = i + 2
  209.             label = -1
  210.             if op in hasjrel:
  211.                 label = i + oparg
  212.             elif op in hasjabs:
  213.                 label = oparg
  214.             
  215.             if label >= 0:
  216.                 if label not in labels:
  217.                     labels.append(label)
  218.                 
  219.             
  220.         label >= 0
  221.     return labels
  222.  
  223.  
  224. def findlinestarts(code):
  225.     '''Find the offsets in a byte code which are start of lines in the source.
  226.  
  227.     Generate pairs (offset, lineno) as described in Python/compile.c.
  228.  
  229.     '''
  230.     byte_increments = [ ord(c) for c in code.co_lnotab[0::2] ]
  231.     line_increments = [ ord(c) for c in code.co_lnotab[1::2] ]
  232.     lastlineno = None
  233.     lineno = code.co_firstlineno
  234.     addr = 0
  235.     for byte_incr, line_incr in zip(byte_increments, line_increments):
  236.         lineno += line_incr
  237.     
  238.     if lineno != lastlineno:
  239.         yield (addr, lineno)
  240.     
  241.  
  242.  
  243. def _test():
  244.     '''Simple test program to disassemble a file.'''
  245.     if sys.argv[1:]:
  246.         if sys.argv[2:]:
  247.             sys.stderr.write('usage: python dis.py [-|file]\n')
  248.             sys.exit(2)
  249.         
  250.         fn = sys.argv[1]
  251.         if not fn or fn == '-':
  252.             fn = None
  253.         
  254.     else:
  255.         fn = None
  256.     if fn is None:
  257.         f = sys.stdin
  258.     else:
  259.         f = open(fn)
  260.     source = f.read()
  261.     if fn is not None:
  262.         f.close()
  263.     else:
  264.         fn = '<stdin>'
  265.     code = compile(source, fn, 'exec')
  266.     dis(code)
  267.  
  268. if __name__ == '__main__':
  269.     _test()
  270.  
  271.